package com.beep.erp.common.dialog;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beep.erp.R;
import com.beep.erp.common.bean.BizStorageLocationsListBean;
import com.beep.erp.common.utils.ScreenUtils;
import com.beep.erp.common.utils.ToastUtils;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.popwindow.SelectDatePop;
import com.beep.erp.common.widget.popwindow.SelectOneTagPopupWindow;

import java.util.ArrayList;


/**
 * Created by Administrator on 2018/9/13.
 */
public class AddStockDialog {
    private AlertDialog dialog;
    private Context context;
    private String locationId;
    private OnCancelClickListener onCancelClickListener;
    private onFullClickListener onFullClickListener;

    public AddStockDialog(Context context) {
        this.context = context;
    }

    /**
     * 提示
     */
    public void showDialog(boolean isCancelable, String differentAmount, ArrayList<BizStorageLocationsListBean.BizStorageLocationsBean> mListBean, boolean isShow) {
        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        dialog.setCancelable(isCancelable);
        dialog.getWindow().clearFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        dialog.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        // 设置布局
        window.setContentView(R.layout.dialog_edit_instock);
        // 设置宽高
        window.setLayout(ScreenUtils.getScreenWidth(context), ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置弹出的动画效果
        //window.setWindowAnimations(R.style.AnimBottom);
        window.setBackgroundDrawableResource(android.R.color.transparent);

        EditText edNum = (EditText) window.findViewById(R.id.et_product_num);
        LinearLayout llDate = (LinearLayout) window.findViewById(R.id.ll_product_date);
        TextView tvDate = (TextView) window.findViewById(R.id.tv_product_date);
        LinearLayout llLocation = (LinearLayout) window.findViewById(R.id.ll_select_location);
        TextView tvSelectLocation = (TextView) window.findViewById(R.id.tv_select_location);
        Button btnSave = (Button) window.findViewById(R.id.btn_save);
        EditText etRemark = (EditText) window.findViewById(R.id.et_product_remark);
        LinearLayout llRemark = (LinearLayout) window.findViewById(R.id.ll_product_remark);
        View line = window.findViewById(R.id.view_line);

        llDate.setVisibility(isShow ? View.VISIBLE : View.GONE);
        llDate.setOnClickListener(view -> {
            hideSoftKeyboard(edNum);
            SelectDatePop selectDatePop = new SelectDatePop(context, tvDate, "");
            selectDatePop.setOnClickOK((year, month, day) -> {
                tvDate.setText(year + "年" + month + "月" + day + "日");
            });
        });
        llLocation.setOnClickListener(view -> {
            hideSoftKeyboard(edNum);
            SelectOneTagPopupWindow selectOneTagPopupWindow = new SelectOneTagPopupWindow(
                    context, tvSelectLocation, "", mListBean, false
            );
            selectOneTagPopupWindow.setOnItemClcik((id, msg) -> {
                locationId = id;
                tvSelectLocation.setText(msg);
            });
        });
        edNum.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (!ToolsUtils.isEmpty(editable.toString())) {
                    int s = Integer.valueOf(editable.toString());
                    int d = Integer.valueOf(differentAmount);
                    if (s == d) {
                        llRemark.setVisibility(View.INVISIBLE);
                        line.setVisibility(View.INVISIBLE);
                    } else {
                        llRemark.setVisibility(View.VISIBLE);
                        line.setVisibility(View.VISIBLE);
                    }
                }
            }
        });

        btnSave.setOnClickListener(view -> {
            String goodsNum = edNum.getText().toString();
            String goodDate = tvDate.getText().toString();
            String goodLocation = tvSelectLocation.getText().toString();
            String remark = etRemark.getText().toString();
            if (ToolsUtils.isEmpty(goodsNum)) {
                ToastUtils.showShort(context.getString(R.string.please_input_goods_num));
                return;
            }
            if (Integer.valueOf(goodsNum)>Integer.valueOf(differentAmount)){
                ToastUtils.showShort(R.string.input_num_tip);
                return;
            }
            if (isShow) {
                if (ToolsUtils.isEmpty(goodDate)) {
                    ToastUtils.showShort(context.getString(R.string.please_select_product_date));
                    return;
                }
            } else {
                goodDate = "";
            }
            if (ToolsUtils.isEmpty(goodLocation)) {
                ToastUtils.showShort(context.getString(R.string.please_select_location));
                return;
            }

            if (dialog != null) {
                dialog.dismiss();
            }
            if (onFullClickListener != null) {
                onFullClickListener.onclick(goodsNum, goodDate, locationId, remark);
            }
        });

    }

    public void hideSoftKeyboard(EditText editText) {
        ((InputMethodManager) context.getSystemService(
                Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                editText.getWindowToken(), 0);
    }

    public void setOnCancelClickListener(OnCancelClickListener listener) {
        this.onCancelClickListener = listener;
    }

    public void setOnFullClickListener(onFullClickListener listener) {
        this.onFullClickListener = listener;
    }

    public interface OnCancelClickListener {
        public void onclick();
    }

    public interface onFullClickListener {
        void onclick(String goodNum, String date, String locationId, String remark);
    }

}
